﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BW.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UpLoadFilesController : ControllerBase
    {

        public UpLoadFilesController(IWebHostEnvironment web)
        {
            Web = web;
        }

        public IWebHostEnvironment Web { get; }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">文件对象</param>
        /// <returns>返回上传文件的虚拟路径</returns>
        [HttpPost]
        public IActionResult UpLoad(IFormFile file)
        {
            //今天口述，明讲
            //物理路径
            //虚拟路径

            string newfilename = Guid.NewGuid().ToString();  // DateTime.Now.ToString("yyyyMMddHHmmss");

            //找到保存文件的位置，【专业】找到保存文件的物理路径
            string path = Web.WebRootPath + @"\pic\"+ newfilename + file.FileName;
            using (FileStream fs=System.IO.File.Create(path))//"文件完整路径"
            {
                file.CopyTo(fs);    //把要上传的文件，装到fs里面
                fs.Close();         //干完活关闭

            }//内存释放

            //返回虚拟路径
            return Ok("/pic/" + newfilename + file.FileName);
        }
    }
}
